package com.trace.pool;

import java.util.concurrent.*;

/**
 * Created by denglw on 2020/1/8.<br/>
 * Desc:
 */
public class ThreadPool {

    public static void main(String[] args) {
        System.out.println(Runtime.getRuntime().availableProcessors());
        ThreadPoolExecutor pool = pool();
        pool.submit(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("执行完成");
        });
        System.out.println(pool.getPoolSize());
        pool.shutdown();
    }

    public static ThreadPoolExecutor pool() {
        BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(100);
        ThreadFactory factory = Executors.defaultThreadFactory();
        RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();
        return new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS, queue, factory, handler);
    }

}
